package edu.ynu.se.xiecheng.achitectureclass.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.dao.BusinessDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ItemDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ShopDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ShopItemDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Business;
import edu.ynu.se.xiecheng.achitectureclass.entity.Shop;
import edu.ynu.se.xiecheng.achitectureclass.entity.Item;
import edu.ynu.se.xiecheng.achitectureclass.entity.ShopItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;


@Service
public class ShopService extends LogicService<ShopDao, Shop,Long> {
    @Resource
    private ItemDao itemDao;
    @Resource
    private ShopDao shopDao;
    @Resource
    private BusinessDao businessDao;
    @Resource
    private ShopItemDao shopItemDao;
    public ShopService(@Autowired ShopDao sd) {
        super(sd);
    }

    public List<ShopItem> listOnShelfShopItem(Long shopId){
        Shop shop = shopDao.getReferenceById(shopId);
        return shopItemDao.findAllByShopAndShelfStatus(shop,1);
    }
    public List<Item> listAvailableItem(Long businessId){
        Business business = businessDao.getReferenceById(businessId);
        List<Shop> shops = getDAO().findAll();
        List<Shop> shopsRemove = getDAO().findAllByBusiness(business);
        shops.removeAll(shopsRemove);
        //获取其他的shop的 set<item>
        List<Item> itemsOther = new ArrayList<>();
        Set<ShopItem> shopItemsOther = new HashSet<>();
        shops.forEach(shopOther->{
            shopItemsOther.addAll(shopItemDao.findAllByShop(shopOther));
        });
        shopItemsOther.forEach(shopItem -> {
            itemsOther.add(shopItem.getItem());
        });
        List<Item> items = itemDao.findAll();
        items.removeAll(itemsOther);
        return items;
    }

    @Transactional
    public Shop createShopItem(Long shopId, Long itemId, Double price){
        //已有item的情况下去做
       //前提是这里item都是这个商家能用的(满足商家不共用商品原则)
        Item item = itemDao.getReferenceById(itemId);
        Shop shop = shopDao.getReferenceById(shopId);
        ShopItem shopItem = shopItemDao.findByItemAndShop(item, shop);
        if(shopItem != null)
        {
            shopItemDao.save(shop.changePrice(shopItem, price));
        }else{
            shopItemDao.save(shop.createShopItem(item, price));
        }
        return shop;
    }

    public ShopItem onShelf(Long shopId, Long itemId){
        Shop shop = getDAO().getReferenceById(shopId);
        Item item = itemDao.getReferenceById(itemId);
        ShopItem shopItem = shopItemDao.findByItemAndShop(item, shop);
        return shopItemDao.save(shop.changeToOnShelfStatus(shopItem));
    }

    public ShopItem offShelf(Long shopId, Long itemId){
        Shop shop = getDAO().getReferenceById(shopId);
        Item item = itemDao.getReferenceById(itemId);
        ShopItem shopItem = shopItemDao.findByItemAndShop(item, shop);
        return shopItemDao.save(shop.changeToOffShelfStatus(shopItem));
    }

    public List<Shop> listShops(){
        return getDAO().findAll();
    }
}
